package com.nzone.posttrip;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private static final String TAG = "ImageAdapter";
    private static final boolean DEBUG = true;
    private Context mContext;
    private String imgData;
    private String geoData;
    private ArrayList<String> thumbsDataList;
    private ArrayList<String> thumbsIDList;
     
    ImageAdapter(Context c){
        mContext = c;
        thumbsDataList = new ArrayList<String>();
        thumbsIDList = new ArrayList<String>();
        getThumbInfo(thumbsIDList, thumbsDataList);
    }
     
    public final Intent callImageViewer(int selectedIndex){
        Intent i = new Intent(mContext, ImagePopup.class);
        String imgPath = getImageInfo(imgData, geoData, thumbsIDList.get(selectedIndex));
        i.putExtra("filename", imgPath);
        
        return i;
    }
    
    public boolean deleteSelected(int sIndex){
        return true;
    }
     
    public int getCount() {
        return thumbsIDList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {   
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        final int fixposition = position;
        
        if (convertView == null){
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(2, 2, 2, 2);
        }else{
            imageView = (ImageView) convertView;
        }

        BitmapFactory.Options bo = new BitmapFactory.Options();
        bo.inSampleSize = 6;
        
        ContentResolver cr = mContext.getContentResolver();
        int id = Integer.parseInt(thumbsIDList.get(fixposition));
        Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MICRO_KIND, bo);
        imageView.setImageBitmap(bitmap);
        
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(callImageViewer(fixposition));
            }
        });
         
        return imageView;
    }
     
    private void getThumbInfo(ArrayList<String> thumbsIDs, ArrayList<String> thumbsDatas){
        
        //query args
        String[] proj = {MediaStore.Images.Media._ID,
                         MediaStore.Images.Media.DATA,
                         MediaStore.Images.Media.DISPLAY_NAME,
                         MediaStore.Images.Media.SIZE,
                         MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                         MediaStore.Images.Media.DATE_TAKEN,
                         MediaStore.Images.Thumbnails._ID};
        
        //filter only today picture
        String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ? ";
        String[] selectionArgs = new String[] {
                "Camera"
        };
        String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";
        
        //get camera image cursor
        Cursor imageCursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                proj, selection, selectionArgs, orderBy);
         
        if (imageCursor != null && imageCursor.moveToFirst()){
            String title;
            String thumbsID;
            String thumbsImageID;
            String thumbsData;
            String thumbsBucket;
            String thumbsDate;
            String data;
            String imgSize;
             
            int thumbsIDCol = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
            int thumbsDataCol = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
            int thumbsImageIDCol = imageCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
            int thumbsSizeCol = imageCursor.getColumnIndex(MediaStore.Images.Media.SIZE);
            int thumbsBucketCol = imageCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            int thumbsDateCol = imageCursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
            
            int num = 0;
            do {
                thumbsID = imageCursor.getString(thumbsIDCol);
                thumbsData = imageCursor.getString(thumbsDataCol);
                thumbsImageID = imageCursor.getString(thumbsImageIDCol);
                imgSize = imageCursor.getString(thumbsSizeCol);
                thumbsBucket = imageCursor.getString(thumbsBucketCol);
                thumbsDate = imageCursor.getString(thumbsDateCol);
                
                Log.d(TAG, thumbsDate);
                num++;
                
                if (thumbsImageID != null /*&& isToday(thumbsDate)*/) {
                    thumbsIDs.add(thumbsID);
                    thumbsDatas.add(thumbsData);
                }
            }while (imageCursor.moveToNext());
        }
        imageCursor.close();
        
        return;
    }
     
    /**
     * 
     * @param Date
     */
    private boolean isToday(String _time) {
        long timeStamp = Long.parseLong(_time);
        String picDate, Today;
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date netDate = new Date(timeStamp);
        
        //pircture time
        picDate = sdf.format(netDate);
        
        //current time
        netDate.setTime(System.currentTimeMillis());
        Today = sdf.format(netDate);
        
        if(DEBUG) {
            Log.d(TAG, "picDate = " + picDate);
            Log.d(TAG, "Today = " + Today);
        }
        
        return picDate == Today;
    }

    private String getImageInfo(String ImageData, String Location, String thumbID){
        String imageDataPath = null;
        String[] proj = {MediaStore.Images.Media._ID,
                 MediaStore.Images.Media.DATA,
                 MediaStore.Images.Media.DISPLAY_NAME,
                 MediaStore.Images.Media.SIZE};
        Cursor imageCursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                proj, "_ID='"+ thumbID +"'", null, null);
         
        if (imageCursor != null && imageCursor.moveToFirst()){
            if (imageCursor.getCount() > 0){
                int imgData = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
                imageDataPath = imageCursor.getString(imgData);
            }
        }
        imageCursor.close();
        return imageDataPath;
    }
}